<?php
namespace Infinity;

class Webform {
  private $node;

  public function __construct($nid)
  {
    if(!module_exists('webform'))
      throw new \Exception('webform module never enabled');
    if(is_numeric($nid))
      $this->node = node_load($nid);
    if(is_object($nid))
      $this->node = $nid;
  }

  /**
   * 新建webofrm組件
   * @param String $type 組件類型
   * @param String $name 組件標題
   * @param String $form_key 組件標識KEY值
   * @param array $options
   * @param Int $cid 組件id 新建則爲NULL
   */
  public function componentNew($type, $name, $form_key, $options=array(), $cid = null)
  {
    require_once(drupal_get_path('module', 'webform').'/includes/webform.components.inc');

    $weight = 0;
    $weights = array();
    if(isset($this->node->webform['components'])) {
      foreach($this->node->webform['components'] as $com) {
        $weights[]= $com['weight'];
      }
      $weight = max($weights) + 1;
    }

    $form_state = array();
    $form_state['values'] = array(
      'type' => $type,
      'nid' => $this->node->nid,
      'cid' => $cid,
      'clone' => false,
      'name' => $name,
      'form_key' => $form_key,
      'required' => 1,
      'pid' => 0,
      'weight' => $weight,
      'extra' => array(
        'description' => '',
        'options_source' => '',
        'multiple' => 0,
        'title_display' => 'inline',
        'private' => 0,
        'wrapper_classes' => '',
        'css_classes' => '',
      )
    );
    if($type == 'select') {
      $form_state['values']['extra']['aslist'] = 0;
      $form_state['values']['extra']['optrand'] = 0;
      if($options) {
        $str_arr = array();
        foreach($options as $key => $val) {
          $str_arr[]= $key.'|'.$val;
        }
        $form_state['values']['extra']['items'] = implode("\r\n", $str_arr);
      }
    }
    if($type == 'textarea') {
      $form_state['values']['extra']['cols'] = '';
      $form_state['values']['extra']['rows'] = '';
      $form_state['values']['extra']['resizable'] = 0;
      $form_state['values']['extra']['placeholder'] = '';
      $form_state['values']['extra']['disabled'] = 0;
    }
    $form['#node'] = $this->node;
    webform_component_edit_form_submit($form, $form_state);
  }

  /**
   * 編輯webform組件
   * @param String $type 組件類型
   * @param String $name 組件標題
   * @param String $form_key 組件標識KEY值
   * @param array $options
   * @param Int $cid 組件id
   */
  public function componentEdit($type, $name, $form_key, $options=array(), $cid) {
    if(!isset($cid)) return;
    $this->componentNew($type, $name, $form_key, $options, $cid);
  }

  public function copyComponentsFrom($nid)
  {
    $query = db_select('webform_component');
    $query->addExpression('MAX(cid)');
    $max = $query->execute()->fetchField();
    $max = $max ?: 1;
    $webformFrom = node_load($nid);
    foreach($webformFrom->webform['components'] as $component) {
      $component['nid'] = $this->node->nid;
      $component['cid'] = ++$max;
      $component['form_key'] = 'copy_'.$GLOBALS['user']->uid.time().rand(1, 999);
      $this->node->webform['components'][] = $component;
    }
    node_save($this->node);
  }
}